home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group99a.txt / 000095_icon-group-sender _Fri Apr 9 08:17:45 1999.msg < prev    next >
Internet Message Format  |  2000-09-20  |  2KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id IAA12257
  4.     for icon-group-addresses; Fri, 9 Apr 1999 08:17:37 -0700 (MST)
  5. Message-Id: <199904091517.IAA12257@baskerville.CS.Arizona.EDU>
  6. From: swampler@noao.edu (Steve Wampler)
  7. Date: Thu, 8 Apr 1999 18:37:14 MST
  8. To: "Hudon, Christian (EXCHANGE:MTL:6X16)" <chudon@americasm01.nt.com>,
  9.         icon-group@optima.CS.Arizona.EDU
  10. Subject: Re: Deep copy in Icon?
  11. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  12. Status: RO
  13.  
  14. > Hi,
  15. > could someone tell me what's wrong with the following snippet of Icon
  16. > code?
  17. > procedure deepcopy(x, t)
  18. >     /t := table()
  19. >     case type(x) of {
  20. >         "list"|"table"|"set"|"record" : {
  21. >             e := t[image(x)]
  22. >             if /e then {
  23. >                 e := copy(x)
  24. >                 t[image(x)] := e
  25. >                 every sub := !e do sub := deepcopy(sub, t)
  26. >                 }
  27. >         return e
  28. >         }
  29. >        default: return x
  30. >     }
  31. > end
  32.  
  33. Hi Christian,
  34.  
  35. This isn't the problem, but you just use t[x] instead of t[image(x)]...
  36.  
  37. Where you're problem shows up is in the "every sub := !e do sub := ..."
  38. line.
  39.  
  40. Immediately after each iteration of "sub := !e" you nicely have
  41. the *value* of the variable sub the same value as in e, but...
  42.  
  43. The assignment of "sub := deepcopy(..." is replacing the value
  44. of sub with a new value, but isn't touching the list e, where
  45. the original value stays untouched.  (i.e. before the assignment,
  46. sub referenced the same value referenced in e, but now the
  47. value of sub references elsewhere).
  48.  
  49. Here's one solution, perhaps someone has a cleaner one:
  50.  
  51. procedure deepcopy(e, t)
  52.  
  53.     /t := table()
  54.     if type(e) == ("list"|"table"|"set"|"record") then {
  55.         e := copy(e)
  56.         every a := e[i := 1 to *e] do {
  57.            /t[a] := deepcopy(a, t)
  58.            e[i] := t[a]
  59.            }
  60.         }
  61.     return e
  62. end
  63.  
  64.  
  65.